package menuService

import (
	"fmt"
	menuDto "news_for_deepseek_interface/dto/admin/system/menu"
	"news_for_deepseek_interface/model"
	adminModel "news_for_deepseek_interface/model/admin"
	"strconv"
	"strings"
)

func MyMenu(params menuDto.MyMenuRequest) (response menuDto.MyMenuResponse, err error) {
	db := model.GetInstance().GetDB().Model(&adminModel.AdminGroupStruct{})

	var adminGroupStruct adminModel.AdminGroupStruct
	if err = db.Where("id = ?", params.Id).First(&adminGroupStruct).Error; err != nil {
		err = fmt.Errorf("获取组信息失败: %v", err)
		return
	}

	if adminGroupStruct.Rules == "*" {
		var menuList []menuDto.ListMenuResponse
		menuList, err = ListMenu(menuDto.ListMenuRequest{})
		if err != nil {
			err = fmt.Errorf("获取菜单失败: %v", err)
			return
		}

		for _, v := range menuList {
			response.Ids = append(response.Ids, v.Id)
		}
	} else {
		rules := strings.Split(adminGroupStruct.Rules, ",")
		for _, v := range rules {
			vInt, _ := strconv.Atoi(v)
			response.Ids = append(response.Ids, vInt)
		}
	}

	return
}
